home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: hex to dec function?
- Date: 22 Feb 96 13:58:24 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.824997504@rscernix>
- References: <4gdh1b$bg@mailhost.mwmicro.com> <4gdolr$ha9@madeline.INS.CWRU.Edu> <4gg3h0INN59e@keats.ugrad.cs.ubc.ca>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4gg3h0INN59e@keats.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
-
- >In article <4gdolr$ha9@madeline.INS.CWRU.Edu>,
- >Michael A. Balfour <mab22@po.CWRU.Edu> wrote:
- > >
- > >In a previous article, aschlies@citynet.net () says:
- > >
- > >>Hi There,
- > >>
- > >>Is there a function that converts HEX to Dec in ANSI C?
- > >>
- > >You could try sscanf. For example:
- > >
- > >main()
- > >{
- > > char hex[9];
- > > int dec;
- > >
- > > strcpy(hex,"FFFF");
- >
- >Of course, someone will change the above to " FFFFF", thus clobbering
- >your activation record, since you comitted the hex array to hold only 9
- >characters.
- >
- >There is no reason why you can't use char *hex = "FFFF"; in such a small
- >example.
-
- For such a small example, sscanf("FFFF", "%x", &dec); would have done the
- job :-)
-
- Note, however, that there are broken implementations where only the original
- example would work, while Kazimir's and mine would segfault! This
- is because string literals _may_ be stored in read-only memory segments
- and some old sscanf implementations attempted to write back (actually
- to ungetc) the first character that couldn't be converted (the terminating
- null in this case). A few years ago, somebody actually posted a similar
- example which crashed on his system and he couldn't understand why.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-